﻿/// <reference path="../typings/breeze/breeze.d.ts" />
/// <reference path="../typings/knockout/knockout.d.ts" />
/// <reference path="SalesOrderEntities.ts" />

module SalesOrderMvvm {
    import b = breeze;
    import ent = SalesOrderEntities;

    export class CustomerVM {
        constructor(
            public customers: KnockoutObservableArray<ent.ICustomer> = ko.observableArray<ent.ICustomer>([]),
            public customer: KnockoutObservable<ent.ICustomer> = ko.observable<ent.ICustomer>(),
            private em: b.EntityManager = new b.EntityManager("http://localhost:49306/breeze/customermanagement")) {
        }

        fetchAllCustomers() {
            b.EntityQuery.from("Customers")
                .using(this.em)
                .execute()
                .then(dt => { this.customers(<ent.ICustomer[]>dt.results); })
                .fail(err => { alert("Problem:" + err.message); });
        }

        saveChanges() {
            this.em.saveChanges();
        }

        public getFirstCustomerName(): string {
            return this.customers()[0].FirstName();
        }
    }
}